package com.master5.grocerylist.credentials;

import java.sql.Connection;
import java.util.Date;
import java.util.Locale;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.master5.grocerylist.ConnectionHelper;
import com.master5.grocerylist.Dao;

public class CredentialsDao extends Dao<Credentials>{
	public int authenticate(String userName, String password) {
		String sql = "SELECT * FROM Credentials WHERE user_account_name = ? AND user_password = ?;";
		Connection c = null;
		
        try {
            c = ConnectionHelper.getConnection();
            PreparedStatement s = c.prepareStatement(sql);
            
            s.setString(1, userName);
            s.setString(2, password);
            ResultSet rs = s.executeQuery(sql);
            
            if (!rs.next())
            	return 0;
            
            return ParseObject(rs).getUserId();
        } catch (SQLException e) {
        	e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
			ConnectionHelper.close(c);
		}
	}
	
	public boolean signUp(String account_name, String password, String name, String surname, String email,
			String phone, String birth_date, String gender, int role_id) {
		Connection c = null;
		String query_user = "INSERT INTO User(user_name, user_surname, user_email, user_phone, user_birthdate, sex) VALUES(?, ?, ?, ?, ?, ?)";
		java.util.Date date = null;
		try {
			date = new SimpleDateFormat("dd.MM.yyyy", Locale.ENGLISH).parse(birth_date);
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			c = ConnectionHelper.getConnection();
			PreparedStatement s = c.prepareStatement(query_user);
			s.setString(1, name);
			s.setString(2, surname);
			s.setString(3, email);
			s.setString(4, phone);
			java.sql.Date birthDate = new java.sql.Date(date.getTime());
			s.setDate(5, birthDate);
			s.setString(6, gender);	
			s.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			;
		}
		String query_user_id = "SELECT * FROM User WHERE user_email LIKE ?";
		
		int user_id;
		try {
			PreparedStatement s = c.prepareStatement(query_user_id);
			s.setString(1, email);
			user_id = ParseObjectToUserID(s.executeQuery());
			if(user_id == 0)
				return false;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			;
		}
		
		String query_credentials = "INSERT INTO Credentials(user_account_name, user_password, user_id) VALUES(?, ?, ?)";
		try {
			PreparedStatement s = c.prepareStatement(query_credentials);
			s.setString(1, account_name);
			s.setString(2, password);
			s.setInt(3, user_id);	
			s.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			;
		}
		//soru 1 user tablosunda role_id mi var :D yok
		
		String query_user_has_role = "INSERT INTO User_has_Role VALUES(?, ?)";
		try {
			PreparedStatement s = c.prepareStatement(query_user_has_role);
			s.setInt(1, user_id);
			s.setInt(2, role_id);		
			s.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			ConnectionHelper.close(c);
		}
		
		
		
		return true;
	}

	@Override
	protected Credentials ParseObject(ResultSet rs) throws SQLException {
		Credentials cred = new Credentials();
		cred.setUserName(rs.getString("user_account_name"));
		cred.setPassword(rs.getString("user_password"));
		cred.setUserId(rs.getInt("user_id"));
		return cred;
	}
	
	protected int ParseObjectToUserID(ResultSet rs) throws SQLException {
		if(rs.next()){
			return rs.getInt("user_id");
		}
		return 0;
	}

}
